/*************************************************************************
	> File Name: Iterator.cc
	>; Author: Joker
	> Mail: mahongtao@foxmail.com 
	> Created Time: 2020-09-22 22:33:22
 ************************************************************************/

#include "Iterator.h"

#include <iostream>

using std::cout;
using std::endl;

/*****************************
 *ConcreateAggregate
 * ****************************/
 
ConcreateAggregate::ConcreateAggregate(int Size)
    :_Size(Size)
     ,_pData(nullptr)
{
    cout << "ConcreateAggregate(int)" << endl;

    _pData = new DATA[_Size];
    for(int i = 0; i < _Size; ++i){
        _pData[i] = i;
    }
}

ConcreateAggregate::~ConcreateAggregate(){
    /* for(int i = 0; i < _Size; ++i){ */
    /*     _pData[i] = i; */
    /*     cout << _pData[i] << " "; */
    /* } */

    delete [] _pData;
    _pData = nullptr;
    cout << "~ConcreateAggregate()" << endl;
}

Iterator * ConcreateAggregate::CreateIterator(Aggregate * pAggregate){
    return new ConcreateIterator(pAggregate);
}

int ConcreateAggregate::GetSize(){
    return _Size;
}

DATA ConcreateAggregate::GetItem(int idx){
    if(idx < _Size){
    /* cout << idx << " : " << _pData[idx] << endl; */
        return _pData[idx];
    }
    else{
        return -1;
    }
}


/***************************
 *ConcreateIterator
 * *************************/
ConcreateIterator::ConcreateIterator(Aggregate * pAggregate)
    :_pConcreateAggregate(pAggregate)
     ,_idx(0)
{
    cout << "ConcreateIterator(Aggregate *)" << endl;
}

ConcreateIterator::~ConcreateIterator(){
    cout << "~ConcreateIterator()" << endl;
}

void ConcreateIterator::First(){
    _idx = 0;
}

void ConcreateIterator::Next(){
    if(_idx < _pConcreateAggregate->GetSize()){
        ++_idx;
    }
}

bool ConcreateIterator::IsDone(){
    return _idx == _pConcreateAggregate->GetSize();
}

DATA ConcreateIterator::CurrentItem(){
    /* cout << "CurrentItem : " << _pConcreateAggregate->GetItem(_idx) << endl; */
    return  _pConcreateAggregate->GetItem(_idx);
}
